import "./style.css";
import * as BABYLON from "babylonjs";
import "babylonjs-loaders";
import { modelsToLoad,modelPath } from "./config.js";
import {
  rightDoorAnim,
  leftDoorAnim,
  backDoorAnim,
  framerate,
} from "./components/animation.js";
import {
  Mt_Body,
  Mt_ABS_Black_Mat,
  Mt_WindScreens,
  Mt_Abs_Black_Gloss,
  Mt_Glass_Lens,
  transparentMaterial,
  Mt_Tyres_Hub,
} from "./components/material.js";

const canvas = document.createElement("canvas");

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

canvas.style.width = window.innerWidth + "px";
document.body.appendChild(canvas);

const engine = new BABYLON.Engine(canvas, true);
export const scene = new BABYLON.Scene(engine);

const camera = new BABYLON.ArcRotateCamera(
  "camera",
  0,
  0,
  0,
  BABYLON.Vector3.Zero(),
  scene,
);

camera.setPosition(new BABYLON.Vector3(0, 5, 10));

camera.attachControl(canvas);

const light0 = new BABYLON.DirectionalLight(
  "light",
  new BABYLON.Vector3(10, -50, -1),
  scene,
);
light0.intensity = 2.5;
const light1 = new BABYLON.DirectionalLight(
  "light",
  new BABYLON.Vector3(-10, -50, -1),
  scene,
);
light1.intensity = 2.5;



var ground = BABYLON.MeshBuilder.CreateGround("ground", {width: 5, height: 5}, scene);

var groundMaterial = new BABYLON.StandardMaterial("groundMaterial", scene);
ground.material = groundMaterial;

var texture = new BABYLON.Texture("./src/assets/images/ground.jpeg", scene); // 替换为你的纹理路径
groundMaterial.diffuseTexture = texture;


const promises = modelsToLoad.map((model) => {
  return new Promise((resolve, reject) => {
    BABYLON.SceneLoader.Append(
      modelPath,
      model,
      scene,
      (scene) => {
        resolve(scene);
      },
      null,
      (error) => {
        reject(error);
      },
    );
  });
});
let rbbox, rfbox, lbbox, lfbox, backbox;
Promise.all(promises)
  .then((res) => {
    // 所有模型加载成功后执行的代码
    console.log("所有模型都已成功加载！", res);
    const transformNodes = scene.transformNodes;

    const meshes = scene.meshes;
    console.info(transformNodes);
    transformNodes.map((item) => {
      if (item.name === "RBDoor") {
        rbbox = new BABYLON.Mesh("box", scene);
        rbbox.status = "close";
        item.setParent(rbbox);
        rbbox.setPivotPoint(new BABYLON.Vector3(0.8, 0, -0.2));
        rbbox.animations.push(rightDoorAnim);
      } else if (item.name === "RFDoor") {
        rfbox = new BABYLON.Mesh("box", scene);
        rfbox.status = "close";
        item.setParent(rfbox);
        rfbox.setPivotPoint(new BABYLON.Vector3(0.8, 0, 0.7));
        rfbox.animations.push(rightDoorAnim);
      } else if (item.name === "LBDoor") {
        lbbox = new BABYLON.Mesh("box", scene);
        lbbox.status = "close";
        item.setParent(lbbox);
        lbbox.setPivotPoint(new BABYLON.Vector3(-0.8, 0, -0.2));
        lbbox.animations.push(leftDoorAnim);
      } else if (item.name === "LFDoor") {
        lfbox = new BABYLON.Mesh("box", scene);
        lfbox.status = "close";
        item.setParent(lfbox);
        lfbox.setPivotPoint(new BABYLON.Vector3(-0.8, 0, 0.7));
        lfbox.animations.push(leftDoorAnim);
      } else if (item.name === "Trunk") {
        backbox = new BABYLON.Mesh("box", scene);
        backbox.status = "close";
        item.setParent(backbox);
        backbox.setPivotPoint(new BABYLON.Vector3(0, 1.4, -1.5));
        backbox.animations.push(backDoorAnim);
      }
    });
    meshes.map((child) => {
      if (child.name.includes("EXT")) {
        switch (child.name) {
          case "EXT_04":
          case "EXT_05":
          case "EXT_08":
          case "EXT_10":
          case "EXT_22":
          case "EXT_23":
          case "EXT_24":
          case "EXT_25":
          case "EXT_28":
          case "EXT_35":
          case "EXT_36":
          case "EXT_37":
          case "EXT_38":
          case "EXT_39":
          case "EXT_40":
          case "EXT_42":
          case "EXT_43":
          case "EXT_44":
          case "EXT_45":
          case "EXT_46":
          case "EXT_47":
          case "EXT_52":
            child.material = Mt_ABS_Black_Mat(scene);
            break;
          case "EXT_06":
          case "EXT_07":
          case "EXT_34":
            child.material = Mt_WindScreens(scene);
            break;
          case "EXT_11":
          case "EXT_21":
          case "EXT_41":
          case "EXT_48":
            child.material = Mt_Abs_Black_Gloss(scene);
            break;
          case "EXT_51":
            child.material = Mt_Body(scene);
            break;
          case "EXT_29":
            break;
          case "EXT_01":
          case "EXT_03":
            child.material = Mt_Body(scene);
            break;

          default:
            child.material = Mt_ABS_Black_Mat(scene);
            break;
        }
      }
      if (child.name.includes("Door")) {
        let doorname = child.name.slice(1);
        switch (doorname) {
          case "BDoor_03":
            child.material = Mt_WindScreens(scene);
            break;
          case "BDoor_02":
          case "BDoor_04":
          case "BDoor_07":
          case "BDoor_08":
          case "BDoor_09":
          case "BDoor_10":
          case "BDoor_14":
            child.material = Mt_ABS_Black_Mat(scene);
            break;
          case "BDoor_05":
            child.material = Mt_Body(scene);
            break;

          case "FDoor_01":
            child.material = Mt_Body(scene);
            break;
          case "FDoor_02":
          case "FDoor_04":
          case "FDoor_05":
            child.material = Mt_ABS_Black_Mat(scene);
            break;
          case "FDoor_08":
            child.material = Mt_WindScreens(scene);
            break;
          case "FDoor_09":
            child.material = Mt_Glass_Lens(scene);
            break;

          default:
            child.material = Mt_ABS_Black_Mat(scene);
            break;
        }
      }
      if (child.name.includes("Sunproof")) {
        switch (child.name) {
          case "Sunproof_01":
          case "Sunproof_03":
            child.material = Mt_WindScreens(scene);
            break;
          default:
            child.material = Mt_ABS_Black_Mat(scene);
            break;
        }
      }
      if (child.name.includes("runk")) {
        switch (child.name) {
          case "Trunk_03":
          case "Trunk_04":
            child.material = Mt_WindScreens(scene);
            break;
          case "Trunk_05":
          case "Trunk_08":
          case "Trunk_09":
          case "Trunk_10":
          case "Trunk_15":
          case "Trunk_19":
          case "Trunk_22":
          case "Trunk_17":
          case "Trunk_93":
            child.material = Mt_ABS_Black_Mat(scene);
            break;
          case "Trunk_12":
            break;
          case "Trunk_92":
            child.material = Mt_Body(scene);
            break;
          default:
            child.material = Mt_Body(scene);
            break;
        }
      }
      if (child.name.includes("Tire")) {
        switch (child.name) {
          case "Tire_01":
          case "Tire_14":
          case "Tire_17":
            child.material = Mt_Body(scene);
            break;
          case "Tire_02":
          case "Tire_08":
          case "Tire_09":
          case "Tire_10":
          case "Tire_12":
            child.material = Mt_ABS_Black_Mat(scene);
            break;
          case "Tire_03":
          case "Tire_05":
            child.material = transparentMaterial(scene);
            break;
          case "Tire_10":
          case "Tire_11":
            child.material = Mt_Tyres_Hub(scene);
            break;
          case "Tire_19":
            child.material = Mt_ABS_Black_Mat(scene);
            break;
          default:
            child.material = Mt_ABS_Black_Mat(scene);
            break;
        }
      }
      if (child.name.includes("INT")) {
        switch (child.name) {
          case "INT_92":
          default:
            child.material = Mt_ABS_Black_Mat(scene);
            break;
        }
      }
    });
    // 在这里执行其他操作
  })
  .catch((error) => {
    // 加载过程中发生错误时的处理
    console.error("模型加载失败:", error);
  });

engine.runRenderLoop(() => {
  scene.render();
});

window.addEventListener(
  "click",
  () => {
    const pickResult = scene.pick(scene.pointerX, scene.pointerY);
    if (pickResult.hit) {
      console.info(pickResult.pickedMesh.id);

      if (pickResult.pickedMesh.id === "RBDoor_05") {
        console.info(rbbox);
        if (rbbox.status === "close") {
          rbbox.status = "open";
          scene.beginAnimation(rbbox, 0, framerate, false);
        } else {
          rbbox.status = "close";
          scene.beginAnimation(rbbox, framerate, 0, false);
        }
      } else if (pickResult.pickedMesh.id === "RFDoor_01") {
        console.info(rbbox);
        if (rfbox.status === "close") {
          rfbox.status = "open";
          scene.beginAnimation(rfbox, 0, framerate, false);
        } else {
          rfbox.status = "close";
          scene.beginAnimation(rfbox, framerate, 0, false);
        }
      } else if (pickResult.pickedMesh.id === "LBDoor_05") {
        console.info(rbbox);
        if (lbbox.status === "close") {
          lbbox.status = "open";
          scene.beginAnimation(lbbox, 0, framerate, false);
        } else {
          lbbox.status = "close";
          scene.beginAnimation(lbbox, framerate, 0, false);
        }
      } else if (pickResult.pickedMesh.id === "LFDoor_01") {
        console.info(rbbox);
        if (lfbox.status === "close") {
          lfbox.status = "open";
          scene.beginAnimation(lfbox, 0, framerate, false);
        } else {
          lfbox.status = "close";
          scene.beginAnimation(lfbox, framerate, 0, false);
        }
      } else if (pickResult.pickedMesh.id.includes("Trunk")) {
        console.info(rbbox);
        if (backbox.status === "close") {
          backbox.status = "open";
          scene.beginAnimation(backbox, 0, framerate, false);
        } else {
          backbox.status = "close";
          scene.beginAnimation(backbox, framerate, 0, false);
        }
      }
    }
  },
  false,
);
